Imports System.Data.SqlClient

Public Class cls_banco_

    Implements ICloneable

#Region "Variables ..."
    Private int_idbanco As Integer = 0
    Private int_idtercero As Integer = 0

    Private obj_tercero As cls_tercero_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_banco_) = Nothing
#End Region

#Region "Propiedades ..."
    Property idbanco() As Integer
        Get
            idbanco = int_idbanco
        End Get
        Set(ByVal value As Integer)
            int_idbanco = value
        End Set
    End Property
    Property idtercero() As Integer
        Get
            idtercero = int_idtercero
        End Get
        Set(ByVal value As Integer)
            int_idtercero = value
        End Set
    End Property
    Property tercero() As cls_tercero_
        Get
            If IsNothing(obj_tercero) Then
                obj_tercero = New cls_tercero_
                If Not obj_tercero.Buscar(idtercero) Then
                    obj_tercero = Nothing
                End If
            End If
            tercero = obj_tercero
        End Get
        Set(ByVal value As cls_tercero_)
            obj_tercero = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_banco_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_banco_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_banco_)
        While obj_Reader.Read()
            Dim Elemento As New cls_banco_
            Elemento.int_idbanco = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idbanco")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
                Elemento.int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idbanco = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idbanco")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
            int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try

            'Creacion de Tercero----------------------------------------------------
            tercero.tipo = cls_tercero_.enu_tipotercero.Cliente
            If tercero.idtercero = 0 Then
                tercero.Nuevo()
            Else
                tercero.Modificar()
            End If
            idtercero = obj_tercero.idtercero
            '-----------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "banco__Nuevo"
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de banco_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try

            'Modificacion de Tercero------------------------------------------------
            obj_tercero.Modificar()
            '-----------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "banco__Modificar"
                .Parameters.Add("@idbanco", SqlDbType.Int).Value = int_idbanco
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de banco_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idbanco As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "banco__Eliminar"
                .Parameters.Add("@idbanco", SqlDbType.Int).Value = idbanco
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de banco_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idbanco As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "banco__Buscar"
                .Parameters.Add("@idbanco", SqlDbType.Int).Value = idbanco
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de banco_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "banco__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de banco_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "banco__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de banco_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_banco_Clon As New cls_banco_
        obj_banco_Clon.int_idbanco = Me.int_idbanco
        obj_banco_Clon.int_idtercero = Me.int_idtercero
        obj_banco_Clon.col_Items = Me.col_Items
        Return obj_banco_Clon
    End Function

End Class
